﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yule8xCommon.Applib;
using Yule8xCommon.Db;
using Yule8xModel;
using Yule8xCommon.Typelib;
using Yule8xWechat.Applib;

namespace Yule8xWechat.Promotion
{
    public partial class Default : System.Web.UI.Page
    {
        protected WxUserInfoResult wxUser = null;
        protected bool isDailyDrawedCard; // 是否已经免费领取过了
        protected int dailyGetDimondNumber; // 每日领卡数量
        protected int refererEachDimondNumber; // 推广普通用户获得钻石数量
        protected int refererDailyMaxDimondNumber; // // 推广玩家每日最大赠卡数量
        protected int user_id;
        protected int game_id_bozhong, game_id_wanghu, bozhongrate, wanghurate;
        protected void Page_Load(object sender, EventArgs e)
        {
            user_id = RequestUtil.GetInteger("user_id");
            if (RuntimeDataUtil.CheckDebugModel() && user_id > 0)
            {
                wxUser = UserModels.GetWxUserInfo(user_id);
                if (null == wxUser)
                {
                    ResponseUtil.WriteEnd("user_id无效!");
                }
            }
            else
            {
                wxUser = LoginPageBase.GetLoginInfo();
                if (null == wxUser)
                {
                    Response.Redirect(WechatUrls.GetUserInfoUrl("promotion"), true);
                }
            }
            /*
            object loginData = Session["login"];
            if (null != loginData)
            {
                wxUser = loginData as WxUserInfoResult;
            }
            if (null == loginData || null == wxUser)
            {
                LogUtil.Instance.WriteInfo("[我的推广/每日免费领卡] 未登录");
                Response.Redirect(WechatUrls.GetUserInfoUrl("promotion"), true);
            }
            */
            bozhongrate = AppConf.GameCurrencyRate_Bozhong;
            wanghurate = AppConf.GameCurrencyRate_Wanghu;

            // 强制要有gameid
            if (wxUser.game_id_bozhong < 1)
            {
                // 获取并同步gameid
                string error;
                wxUser.game_id_bozhong = UserModels.GetGameID(wxUser.user_id, null,"bozhong",out error);
                if (wxUser.game_id_bozhong < 1)
                {
                    Response.Redirect("NoGameID.aspx", true);
                }
                else
                {
                    Session["login"] = wxUser; // 重新保存带有gameid的会话消息
                }
            }
            LogUtil.Instance.WriteInfo("[我的推广/每日免费领卡] 已登录");
            int uid = wxUser.user_id;
            
            dailyGetDimondNumber = Applib.AppConf.DailyCardCount; // 每日领卡数量

            // 推广玩家每日最大赠卡数量
            refererEachDimondNumber = Applib.AppConf.ReferralUserEachCardCount; // 推广普通用户获得钻石数量
            int refereralMaxDimondNumber = Applib.AppConf.ReferralUserDailyMaxCardCount; // 推广玩家每日最大赠卡数量
            refererDailyMaxDimondNumber = UserModels.GetReferralUserDailyMaxNumber(uid, DateTime.Today, refereralMaxDimondNumber);
            // 判断每日领卡状态
            isDailyDrawedCard = UserModels.CheckUserDailyDrawDimondStatus(uid);
            string action = RequestUtil.GetString("action",maxLen:16);
            if ("drawcard" == action)
            {
                if (isDailyDrawedCard)
                {
                    ResponseUtil.AjaxWrite("{'info':'您今天已经领过钻石了，请勿重复领取!','status':false}".Replace("'", "\""));
                }else
                {
                    string outputMsg;
                    bool result = UserModels.UserDailyGetDimond(uid, DateTime.Today, dailyGetDimondNumber, dailyGetDimondNumber, wxUser.game_id_bozhong, 0, out outputMsg);
                    
                    LogUtil.Instance.WriteInfo(string.Format("[我的推广/每日免费领卡] UserModels.UserDailyGetDimond({0}, DateTime.Today, {1}, {2}, 0, out outputMsg); {3}: {4}", uid, dailyGetDimondNumber, wxUser.game_id_bozhong,result,outputMsg));

                    ResponseUtil.AjaxWrite("{'info':'领取成功!','status':true}".Replace("'", "\""));
                }
            }
        }
    }
}